package com.xm.xmapp.fragment;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.myinterface.MainInterFaceAble;
import com.xm.xmapp.tool.AndroidTool;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LDW on 2017/7/19.
 */
public class MainTopFragment extends BaseFragment {


    @Bind(R.id.tv_short_rent)
    TextView tvShortRent;
    @Bind(R.id.tv_long_rent)
    TextView tvLongRent;
    @Bind(R.id.tv_pile)
    TextView tvPile;
    @Bind(R.id.iv_cursor)
    ImageView ivCursor;
    @Bind(R.id.iv_person)
    ImageView ivPerson;
    @Bind(R.id.iv_search)
    ImageView ivSearch;

    private int offset = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度
    int itemV;
    private MainInterFaceAble mMainInterFaceAble;

    public void setmMainInterFaceAble(MainInterFaceAble mMainInterFaceAble) {
        this.mMainInterFaceAble = mMainInterFaceAble;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_maintop, null, false);
        ButterKnife.bind(this, view);

        ViewTreeObserver vto2 = view.getViewTreeObserver();
        vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                bmpW = BitmapFactory.decodeResource(getResources(), R.mipmap.home_line_test)
                        .getWidth();// 获取图片宽度

                int screenW = AndroidTool.getScreenWidth(getActivity()) -ivPerson.getWidth() - ivSearch.getWidth();// 获取分辨率宽度
                itemV = screenW / 3;
                int offset = (itemV - bmpW) / 2 + ivPerson.getWidth();// 计算偏移量
                Matrix matrix = new Matrix();
                matrix.postTranslate(offset, 0);
                ivCursor.setImageMatrix(matrix);// 设置动画初始位置


            }
        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_short_rent, R.id.tv_long_rent, R.id.tv_pile,R.id.iv_person, R.id.iv_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_short_rent:
                if (currIndex != 0)
                    changeImage(0);
                break;
            case R.id.tv_long_rent:
                if (currIndex != 1)
                    changeImage(1);
                break;
            case R.id.tv_pile:
                if (currIndex != 2)
                    changeImage(2);
                break;
            case R.id.iv_person:
                mMainInterFaceAble.uIChange(MainInterFaceAble.SHOW_LEFT,null);
                break;
            case R.id.iv_search:
                break;
        }
    }

    private void changeImage(int select) {
        Animation animation = null;
        switch (select) {
            case 0:
                if (currIndex == 1) {
                    animation = new TranslateAnimation(itemV, 0, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(2*itemV, 0, 0, 0);
                }
                break;
            case 1:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(0, itemV, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(2*itemV,itemV, 0, 0);
                }
                break;
            case 2:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(0, 2*itemV, 0, 0);
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(itemV, 2*itemV, 0, 0);
                }
                break;
        }
        currIndex = select;
        animation.setFillAfter(true);// True:图片停在动画结束位置
        animation.setDuration(300);
        ivCursor.startAnimation(animation);
        mMainInterFaceAble.uIChange(MainInterFaceAble.NAVIGATION,currIndex);

    }

}
